package com.google.gwt.hmconsole.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.hmconsole.client.to.BrowserConnectionInfoTO;
import com.google.gwt.hmconsole.client.to.HostedModeStateTO;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;

import java.util.ArrayList;

public class HostedModeConsole implements EntryPoint {

  private static final String SERVER_ERROR = "An error occurred while "
      + "attempting to contact the server. Please check your network "
      + "connection and try again.";

  private final HostedModeServiceAsync rpc = GWT.create(HostedModeService.class);

  public void onModuleLoad() {
    final RootPanel tabContainer = RootPanel.get("tabContainer");
    tabContainer.add(new Label("Loading..."));

    rpc.getHostedModeState(new AsyncCallback<HostedModeStateTO>() {

      public void onSuccess(HostedModeStateTO result) {
        tabContainer.clear();
        TabPanel browserTabs = new TabPanel();

        ArrayList<BrowserConnectionInfoTO> connections = result.getBrowserConnections();
        for (BrowserConnectionInfoTO conn : connections) {
          browserTabs.add(new Label("A browser"), conn.getBrowserName());
        }

        tabContainer.add(browserTabs);
      }

      public void onFailure(Throwable caught) {
        tabContainer.clear();
        tabContainer.add(new Label(SERVER_ERROR));
      }
    });
  }
}
